const calcul = goodses => {
  let money = 0;
  for (let i = 0; i < goodses.length; i++) {
    if (goodses[i]) {
      money += goodses[i].goods.priceReal * goodses[i].total;
    }
  }
  return money;
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
var config = require('config.js')



const isLogin = () => {
  console.log('登陆了吗', wx.getStorageSync('token'), wx.getStorageSync('userId'), wx.getStorageSync('token') && wx.getStorageSync('userId'))
  if (wx.getStorageSync('token') && wx.getStorageSync('userId')) {
    wx.request({
      url: config.domain + '/user?id=1',
      header: {
        'token': wx.getStorageSync('token')
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log("test", res)
        if (res.data.code != 200) {
          wx.navigateTo({
            url: '/pages/login/login?isRegister=0',
          })
        }
        return
      },
      fail: function (err) {
        wx.hideLoading();
        wx.showToast({
          title: "服务器出了点小差",
          icon: "none"
        });
      }
    })
  } else {
    console.log("没登录")
    wx.navigateTo({
      url: '/pages/login/login?isRegister=0',
    })
  }
}

const isLoginAns = () => {
  if (wx.getStorageSync('token') && wx.getStorageSync('userId')) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: config.domain + '/user?id=1',
        header: {
          'token': wx.getStorageSync('token')
        },
        method: 'GET',
        dataType: 'json',
        success: function (res) {
          if (res.data.code != 200) {
            resolve(false); // 如果是200以外的码，就返回false
          } else {
            resolve(true); // 如果是200，就返回true
          }
        },
        fail: function (err) {
          resolve(false); // 如果有错误，也返回false
        }
      });
    });
  } else {
    return false; // 如果没有token或userId，直接返回false
  }
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatHtml = content => {
  content = content.replace(/\<img/gi, '<img style="width:100% !important;height:auto !important;margin:0;display:flex;" ');
  content = content.replace(/\<td/gi, '<td  cellspacing="0" cellpadding="0" border="0" style="display:block;vertical-align:top;margin: 0px; padding: 0px; border: 0px;outline-width:0px;" ');
  content = content.replace(/width=/gi, 'sss=');
  content = content.replace(/height=/gi, 'sss=');
  content = content.replace(/ \/\>/gi, ' style="max-width:100% !important;height:auto !important;margin:0;display:block;" \/\>');
  return content;
}

/**
 * 移除购物车Tabbar的数字
 */
const removeTabBadge = () => {
  wx.removeTabBarBadge({
    index: 2
  })
}

/**
 * 判断是否登录，否就跳转
 */

module.exports = {
  formatTime: formatTime,
  formatHtml: formatHtml,
  removeTabBadge: removeTabBadge,
  isLogin: isLogin,
  calcul: calcul,
  isLoginAns: isLoginAns
}